﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using FacebookXmpp;
using XFControls.XFPanels;
using XDAFacebook;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XFItemControls.XFPanels.XFPanelItems;
using XFSenseUI;
using XFSenseUI.XFItems;
using XFSenseUI.XFItems.SenseControls;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace XFItemControls.XFPanels
{
    public partial class XFChatConversationHistory : SenseItemBase
    {
        private List<Response> _messages = new List<Response>();
        private Person _person;

        public XFChatConversationHistory(Person person)
        {
            _height = 200;
            _person = person;

            Style = new XFItemStyle
            {
                PaddingTop = 2,
                BackgroundColor = Color.RoyalBlue,
                TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold),
                TextColor = Color.White,
                DashStyleBottom = DashStyle.Dash
            };

            ItemType = XFItemType.Display;
        }

        

        public void AddRange(IEnumerable<Response> responses)
        {
            _messages.AddRange(responses);
            Invalidate();
        }

        public XFChatConversationHistory(IContainer container)
        {
          //  container.Add(this);

            //InitializeComponent();
        }

        private int _height;
        public override int GetHeight()
        {
            return _height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            int width = 220;
            int height = 400;
            
            Brush white = new SolidBrush(Color.FromArgb(255, 255, 255));
            g.FillRectangle(white, 0, 0, width, height);

            /*
            g.DrawLine(new Pen(Color.Red), 0, 0, width, height);
           g.DrawLine(new Pen(Color.Red), width, 0, 0, height);
           */
            Font f = new Font("Arial", 10, FontStyle.Regular);

            Brush black = new SolidBrush(Color.FromArgb(30,30,30));
            Brush blue = new SolidBrush(Color.FromArgb(0x20,0x50,0xf0));

            float left;
            float index = 0;
            float textHeight = f.MeasureString("X", new Rectangle(0,0,200,200)).Height;

            foreach (var msg in _messages)
            {
                
                SizeF size = g.MeasureString(msg.Message, f);
                if (size.Width > (float)width)
                {

                    int start = 0;
                    int end = msg.Message.Length;

                    while (start < end)
                    {
                        int cut = 0;
                        // no SPACE so cut the string by working out how many XX's fit across the screen
                        if (msg.Message.IndexOf(' ', start) == -1)
                            cut = ((int)(width / g.MeasureString("X", f).Width)) - 1;
                        else
                        {
                            int test = end;
                            size.Width = width + 10;
                            
                            while ((int)size.Width > width)
                            {
                                test = msg.Message.LastIndexOf(' ', test - 1);
                                if (test == -1 || test == start)
                                {
                                    test = end;
                                    break;
                                }
                                
                                size = g.MeasureString(msg.Message.Substring(start, test - start - 1), f);
                             }

                            if (start != 0) start++;
                            cut = test - start;
                            
                        }

                         left = (msg.From == _person) ? 0 : (float)width - size.Width - 10;
                    
                        g.DrawString(msg.Message.Substring(start, cut), f, (msg.From == _person ? blue : black), left, index);
                        index += textHeight;
                        if (index > _height)
                            break;

                        start += cut;
                    }


                }
                else
                {
                    size = g.MeasureString(msg.Message, f);
                     left = (msg.From == _person) ? 0 : (float)width - size.Width;
                    g.DrawString(msg.Message, f, (msg.From == _person ? blue : black), left, index);
                    index += textHeight;
                }

                if (index > _height)
                    break;

            }

        }

        

    }
}
